package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;

/**
 * 给定时任务配置线程池
 * <p>
 * 参考: https://www.cnblogs.com/goodAndyxublog/p/12244729.html
 *
 * @author 杜云山
 * @date 22/06/01
 */
@Configuration
public class SchedulerConfiguration {

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {

        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.setAwaitTerminationSeconds(60);
        taskScheduler.setThreadGroupName("book-task-");
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        taskScheduler.setRemoveOnCancelPolicy(true);
        taskScheduler.initialize();

        return taskScheduler;
    }

    /**
     * tomcat 请求头太大的问题 : https://blog.csdn.net/yueaini10000/article/details/107809001
     * https://developer.aliyun.com/article/629236
     *
     * @return /
     */
    @Bean
    public RestTemplate restTemplate() {

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(5000);
        requestFactory.setReadTimeout(3000);

        return new RestTemplate(requestFactory);
    }

}
